<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="原密码:" prop="originalPassword">
      <el-input type="password" v-model="ruleForm.originalPassword" placeholder="请输入原始密码"></el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input type="password" v-model="ruleForm.newPassword" placeholder="请输入新密码"></el-input>
    </el-form-item>

    <el-form-item label="确认密码" prop="checkPassword">
      <el-input type="password" v-model="ruleForm.checkPassword" placeholder="请再次确认密码"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="changePassword">修改</el-button>
      <el-button @click="reset">重置</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
export default {
  name: "ChangePassword",
  data() {
    return {
      ruleForm: {
        originalPassword: '',
        newPassword: '',
        checkPassword:'',
      },
      rules: {
        originalPassword: [
          { required: true, message: '请输入原密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, message: '请再次确认密码', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    changePassword: function(){
      const _this = this;
      if(_this.ruleForm.originalPassword == localStorage.getItem('password')) {
        const _this = this;
        if (_this.ruleForm.newPassword == _this.ruleForm.checkPassword) {

          const _this = this;
          this.$axios.post('/systemuser/update', {
            "id" : localStorage.getItem('id'),
            "userName" : localStorage.getItem('userName'),
            "password" : _this.ruleForm.checkPassword,
            "realName" : localStorage.getItem('realName'),
            "sex" : localStorage.getItem('sex'),
            "email" : localStorage.getItem('email'),
            "phone" : localStorage.getItem('phone'),
            "description" : localStorage.getItem('description')
          }, {
            headers: {
              "Content-Type": "application/json;charset=utf-8"
            },
            withCredentials: true
          }).then(function (/*response*/) {
            localStorage.setItem("password",_this.ruleForm.checkPassword);

            let flag = false;
            _this.$store.commit('login', flag);
            localStorage.clear();
            console.log(_this.$route.path);
            _this.$message({
              message: '修改密码成功！',
              type: 'success'
            });
            _this.$router.push({name: "Login", params: {isReload: 'true'}});
          }).catch(function (response) {
            // 这里是处理错误的回调
            _this.$message.error(response.data.message)
          })
        } else {
          _this.$message({
            message: '两次输入的密码不一致',
            type: 'error'
          });
        }
      }
      else{
        _this.$message.error("您输入的原密码错误！");
      }
    },
    reset: function(){
      this.ruleForm.originalPassword = '';
      this.ruleForm.newPassword = ''
      this.ruleForm.checkPassword = '';
    }
  }
}
</script>

<style scoped>

</style>
